﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyPhongKham
{
    public partial class ucGiaKham2 : BaseUserControl
    {
        private DAL.AccessData m;
        private int i_only;
        private DataTable dt = new DataTable();


        public ucGiaKham2(DAL.AccessData acc)
        {
            this.m = acc;
            InitializeComponent();
            this.Title = "Giá Viện Phí";
        }


        private void frmGiakham_Load(object sender, EventArgs e)
        {
            LoadComboBox();
            load_grid();
            reft_text();
            //EnableButtons(true);
        }
        /// <summary>
        /// Load items into comobox
        /// </summary>
        private void LoadComboBox()
        {
            DataTable dtform = new DataTable();
            dtform.Columns.Add("form", typeof(string));
            dtform.Columns.Add("ten", typeof(string));
            dtform.Rows.Add(new string[] { "pKhamvu", "Khám vú" });
            dtform.Rows.Add(new string[] { "pKhamthai", "Khám thai" });
            dtform.Rows.Add(new string[] { "pKhampk", "Khám phụ khoa" });
            dtform.Rows.Add(new string[] { "pHiemmuon", "Khám hiếm muộn" });
            dtform.Rows.Add(new string[] { "pSieuam", "Siêu âm 4D" });
            dtform.Rows.Add(new string[] { "pSieuam_th", "Siêu âm thai" });
            dtform.Rows.Add(new string[] { "pSieuam_tn", "Siêu âm thai nghén" });
            dtform.Rows.Add(new string[] { "pSieuam_pk", "Siêu âm đầu dò âm đạo" });
            dtform.Rows.Add(new string[] { "pCLS1", "Làm phiến đồ tế bào" });
            dtform.Rows.Add(new string[] { "pCLS2", "Soi tưoi" });
            dtform.Rows.Add(new string[] { "pCLS3", "Soi cổ tử cung kỹ thuật số" });
            dtform.Rows.Add(new string[] { "pCLS4", "Đặt thuốc" });
            dtform.Rows.Add(new string[] { "pTheodoi", "Theo dõi tim thai bằng máy" });
            dtform.Rows.Add(new string[] { "pSieuam4D", "Siêu âm màu Dopler 4D" });
            dtform.Rows.Add(new string[] { "pSieuam_12", "Siêu âm 4D thai 12-14 tuần" });
            formnhap.DisplayMember = "TEN";
            formnhap.ValueMember = "FORM";
            formnhap.DataSource = dtform;
        }

        private void MaskDigit(KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || e.KeyChar == 8 || e.KeyChar == '.') e.Handled = false;
            else e.Handled = true;
        }
        /// <summary>
        /// Load grid gia vien phi
        /// </summary>
        private void load_grid()
        {
            dt = m.get_data("select * from " + m.user + ".dmgiavp where id<>-9999").Tables[0];
            this.gridControl1.DataSource = dt;
        }
       
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            //try
            //{
            //    if (row == dataGrid1.CurrentRowIndex)
            //    {
            //        dataGrid1.Select(row);
            //        c = Color.White;
            //    }
            //}
            //catch
            //{
            //}
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);


        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }

        

        private void butMoi_Click(object sender, EventArgs e)
        {
            tenpk.Enabled = true; formnhap.Enabled = true; dongia.Enabled = true; pttt.Enabled = true;
            tenpk.Text = ""; dongia.Text = "0"; pttt.Checked = false;
            //EnableButtons(false);
            mapk.Text = m.get_capid_giavp.ToString();
            tenpk.Focus();
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            if (tenpk.Text == "")
            {
                tenpk.Focus(); return;
            }
            if (dongia.Text == "")
            {
                dongia.Focus(); return;
            }
            if (mapk.Text != "")
            {
                m.upd_dmgiavp(long.Parse(mapk.Text), tenpk.Text, decimal.Parse(dongia.Text), formnhap.SelectedValue.ToString(), (pttt.Checked) ? 1 : 0);
                tenpk.Enabled = false; dongia.Enabled = false; formnhap.Enabled = false; pttt.Enabled = false;
                load_grid();
                //EnableButtons(true);
                reft_text();
                //butMoi.Focus();
            }
        }

        private void reft_text()
        {
            try
            {
                //tenpk.Enabled = false; formnhap.Enabled = false; dongia.Enabled = false; pttt.Enabled = false;
                //mapk.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString();
                //tenpk.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 1].ToString();
                //decimal d_dongia;
                //d_dongia = decimal.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 2].ToString());
                //dongia.Text = d_dongia.ToString("###,###,##0");
                //formnhap.SelectedValue = dataGrid1[dataGrid1.CurrentCell.RowNumber, 3].ToString();
                //i_only = int.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 4].ToString());
                //pttt.Checked = int.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 5].ToString()) == 1;
            }
            catch { }
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            reft_text();
        }

        private void butBoqua_Click(object sender, EventArgs e)
        {
            tenpk.Enabled = false; dongia.Enabled = false; formnhap.Enabled = false; pttt.Enabled = false;
            reft_text();
            //EnableButtons(true);
        }

        private void butSua_Click(object sender, EventArgs e)
        {
            tenpk.Enabled = true; dongia.Enabled = true; formnhap.Enabled = true;
            tenpk.Focus(); pttt.Enabled = true;
            
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (i_only == 1)
            {
                MessageBox.Show("Danh mục đã khoá, không được huỷ! ", DAL.AccessData.Msg);
                return;
            }
            m.execute_data("delete from " + m.user + ".dmgiavp where id=" + long.Parse(mapk.Text));
            load_grid();
            reft_text();
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void dongia_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskDigit(e);
        }

        private void tenpk_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}{F4}");
            }
        }

        private void dataNavigator1_Click(object sender, EventArgs e)
        {
            //this.dataNavigator1.
        }

        private void crudOperation1_ButtonClick(object sender, DBOperateEventArgs e)
        {
            switch (e.ButtonKind)
            { 
                case ButtonType.New:
                    butMoi_Click(null, null);
                    break;
                case ButtonType.Save:
                    butLuu_Click(null, null);
                    break;
                case ButtonType.Edit:
                    butSua_Click(null, null);
                    break;
                case ButtonType.Delete:
                    butHuy_Click(null, null);
                    break;
                case ButtonType.Ignore:
                    butBoqua_Click(null, null);
                    break;default: break;
            }
        }
    }
}
